//
//  J智现场
//
//  Created by song on 2017/11/13.
//  Copyright © 2017年 王长飞. All rights reserved.
//

#import "FriendsCodeController.h"
#import <WebKit/WebKit.h>
#import "FriendsCodeModel.h"

@interface FriendsCodeController ()<BaseModelProtocol,UIActionSheetDelegate,UIGestureRecognizerDelegate>

@property(nonatomic,strong)FriendsCodeModel *codeModel;
@property(nonatomic,strong)UIImageView *sentImg;

@end

@implementation FriendsCodeController


- (void)initView
{
    [self.navBarView addTitle:@"邀请好友"];
    [super initView];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.codeModel getCodeData];
    
}

- (void)getDataFinish:(BaseModel *)baseModel netResponse:(BaseResponse *)response{
    FriendsCodeResponse *resp = (FriendsCodeResponse *)response;
    FriendsCodeData *data = resp.resObject;
    [self createUI:data];
}

- (void)createUI:(FriendsCodeData *)data{
    UIImageView *bgView = [[UIImageView alloc]initWithFrame:CGRectMake(0,KNavFullHeight, SCREEN_WIDTH, SCREEN_HEIGHT_EXCEPTNAV-60)];
    bgView.userInteractionEnabled = YES;
    [bgView sd_setImageWithURL:[NSURL URLWithString:data.backgroundPic] placeholderImage:kDefaultImage];
    [bgView addTapCallBack:self sel:@selector(imglongTapClick:)];
    [self.view addSubview:bgView];

//    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(imglongTapClick:)];
//    ///设置长按手势最小按压时间
//    longPress.minimumPressDuration = 2;
//    [bgView addGestureRecognizer:longPress];
    
    UILabel *titleLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 40, bgView.width, 50)];
    titleLab.text = data.buttonText;
    titleLab.textColor = UIColorFromRGB1(0x008DD7);
    titleLab.font = [UIFont systemFontOfSize:26];
    titleLab.textAlignment = NSTextAlignmentCenter;
    [bgView addSubview:titleLab];
    
    UIView *centerBgView = [[UIView alloc]initWithFrame:CGRectMake((SCREEN_WIDTH-220)/2,titleLab.bottom+30, 220, 220)];
    centerBgView.userInteractionEnabled = YES;
    centerBgView.backgroundColor = [UIColor whiteColor];
    [centerBgView setCornerRedius:6];
    [bgView addSubview:centerBgView];
    
    UILabel *centerLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, centerBgView.width, 30)];
    centerLab.text = @"长按二维码保存到相册";
    centerLab.textColor = kTitleColor_midden;
    centerLab.font = kFontMidden;
    centerLab.textAlignment = NSTextAlignmentCenter;
    [centerBgView addSubview:centerLab];
    
    UIImageView *codeImgView = [[UIImageView alloc]initWithFrame:CGRectMake((centerBgView.width-130)/2,centerLab.bottom+20, 130, 130)];
    [codeImgView sd_setImageWithURL:[NSURL URLWithString:data.qrcodeUrl] placeholderImage:kDefaultImage];
    [centerBgView addSubview:codeImgView];
    
    codeImgView.userInteractionEnabled = YES;
//    [codeImgView addTapCallBack:self sel:@selector(imglongTapClick:)];

    UILabel *bottomLab = [[UILabel alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT-60, bgView.width, 60)];
    bottomLab.text = @"我的邀请";
    bottomLab.font = [UIFont systemFontOfSize:22];
    bottomLab.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:bottomLab];
}

//-(void)imglongTapClick:(UILongPressGestureRecognizer *)gesture
-(void)imglongTapClick:(UIGestureRecognizer *)gesture
{
//    if(gesture.state == UIGestureRecognizerStateBegan)
//    {
        UIActionSheet *actionSheet = [[UIActionSheet alloc]

                                      initWithTitle:@"保存图片"

                                      delegate:self

                                      cancelButtonTitle:@"取消"

                                      destructiveButtonTitle:nil

                                      otherButtonTitles:@"保存图片到手机",nil];

        actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;

        [actionSheet showInView:self.view];
    
//        UIImageView *codeImg = (UIImageView *)[gesture view];
//        _sentImg = codeImg;

//    }

}

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
//        UIImageWriteToSavedPhotosAlbum(_sentImg.image, self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), nil);
        [self screenshot];
    }
}

- (void)screenshot{
    
    //获取图形上下文
    UIGraphicsBeginImageContext(self.view.frame.size);
    //将view绘制到图形上下文中
    [[self.view layer] renderInContext:UIGraphicsGetCurrentContext()];
    
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    CGImageRef imageRef = viewImage.CGImage;
    
    //这里可以设置想要截图的区域
//    CGRect rect = CGRectMake((SCREEN_WIDTH-100)/2-20, 64+100-20, 140,140);
    CGRect rect = CGRectMake(0, KNavFullHeight, SCREEN_WIDTH,SCREEN_HEIGHT-KNavFullHeight);
    CGImageRef imageRefRect = CGImageCreateWithImageInRect(imageRef, rect);
    UIImage *sendImage = [[UIImage alloc] initWithCGImage:imageRefRect];
    //将截屏保存到相册
//    UIImageWriteToSavedPhotosAlbum(newImage,self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    UIImageWriteToSavedPhotosAlbum(sendImage, self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), nil);
}

#pragma mark --- UIActionSheetDelegate---

- (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    NSString *message = @"呵呵";
    if (!error) {
        message = @"成功保存到相册";
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alert show];
    }else
    {
        message = [error description];
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alert show];
    }
}

//- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
//{
//    if (error) {
//        [EFShowView showErrorText:@"保存失败，请检查是否拥有相关的权限"];
//    }
//    else{
//        [EFShowView showErrorText:@"保存成功！"];
//    }
// }

- (FriendsCodeModel *)codeModel{
    
    if (nil == _codeModel) {
        _codeModel = [[FriendsCodeModel alloc]initWithDelegate:self cache:NO];
    }
    return _codeModel;
}

@end
